package com.or.trail;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.or.app.StateApplication;
import com.or.service.TrailSaveService;
import com.or.utils.C;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private long lastbacktime;
    private StateApplication app;
    private Button btn_start;
    private TextView tv_log;
    private EditText et_jg;
    private TextView tv_gpsinfo;
    private TextView tv_gpssnr;
    private TextView tv_gpsstate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        app = (StateApplication) getApplication();
        IntentFilter bdr_filter = new IntentFilter();
        bdr_filter.addAction(C.BDR_ACTION_STATE);
        bdr_filter.addAction(C.BDR_ACTION_LOG);
        registerReceiver(bdr, bdr_filter);
        btn_start = (Button) findViewById(R.id.btn_start);
        tv_log = (TextView) findViewById(R.id.tv_logs);
        tv_gpsinfo = (TextView) findViewById(R.id.tv_gpsinfo);
        tv_gpsstate = (TextView) findViewById(R.id.tv_gpsstate);
        tv_gpssnr = (TextView) findViewById(R.id.tv_gpssnr);
        et_jg = (EditText) findViewById(R.id.et_jg);
        btn_start.setOnClickListener(this);
        btn_start.setText(app.getState() ? "停止" : "启动");
        tv_gpsstate.setBackgroundColor(Color.RED);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(bdr);
    }

    @Override
    public void onClick(View v) {
        btn_start.setEnabled(false);
        String title = btn_start.getText().toString();
        if ("启动".equals(title)) {/*启动*/
            btn_start.setText("正在启动...");
            Intent intent = new Intent(MainActivity.this, TrailSaveService.class);
            intent.putExtra("t", Integer.valueOf(et_jg.getText().toString()) * 1000);
            startService(intent);
        } else {/*停止*/
            btn_start.setText("正在停止...");
            stopService(new Intent(MainActivity.this, TrailSaveService.class));
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {/*按两次back退出程序*/
            if (lastbacktime == 0 || System.currentTimeMillis() - lastbacktime > 1000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                lastbacktime = System.currentTimeMillis();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void appendLog(String log) {
        String time = DateFormat.format("MM/dd hh:mm:ss", System.currentTimeMillis()).toString();
        tv_log.append(Html.fromHtml("<font color=\"#ff0000\">" + time + "</font>:" + log + "<br>"));
    }

    private void doMsg(Intent intent) {
        int type = intent.getIntExtra("type", 0);
        switch (type) {
            case 1:
                appendLog(intent.getStringExtra("log"));
                break;
            case 2:
                tv_gpsinfo.setText(intent.getStringExtra("gpsinfo"));
                break;
            case 3:
                if (intent.getBooleanExtra("gpsinfo", false)) {
                    tv_gpsstate.setBackgroundColor(Color.GREEN);
                } else {
                    tv_gpsstate.setBackgroundColor(Color.RED);
                }
                break;
            case 4:
                tv_gpsstate.setText(intent.getIntExtra("savecount", 0) + "");
                break;
            case 5:
                tv_gpssnr.setText(intent.getStringExtra("gpssnr"));
                break;
        }
    }

    private BroadcastReceiver bdr = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case C.BDR_ACTION_LOG: {
                    doMsg(intent);
                }
                break;
                case C.BDR_ACTION_STATE: {
                    StateApplication sapp = (StateApplication) getApplication();
                    if (sapp.getState()) {
                        btn_start.setText("停止");
                    } else {
                        btn_start.setText("启动");
                        tv_gpsstate.setBackgroundColor(Color.RED);
                    }
                    btn_start.setEnabled(true);
                }
                break;
                case C.BDR_ACTION_GPS:/*gps 状态值*/ {

                }
                break;
            }
        }
    };


}
